package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.CoursesDO;
import com.jinzhi.jzweb.domain.TrainingPapersDO;
import com.jinzhi.jzweb.domain.TrainingResultsDO;
import com.jinzhi.jzweb.service.CoursesService;
import com.jinzhi.jzweb.service.TrainingPapersService;
import com.jinzhi.jzweb.service.TrainingResultsService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 培训成绩表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trainingResults")
public class TrainingResultsController extends BaseController {
	@Autowired
	private TrainingResultsService trainingResultsService;
	@Autowired
	private TrainingPapersService trainingPapersService;
	@Autowired
	private CoursesService coursesService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:trainingResults:trainingResults")
	String TrainingResults(){
	    return "jzweb/trainingResults/trainingResults";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:trainingResults:trainingResults")
	public Result<Page<TrainingResultsDO>> list(HttpServletRequest req, TrainingResultsDO trainingResultsDTO){
        Wrapper<TrainingResultsDO> wrapper = new EntityWrapper<TrainingResultsDO>().orderBy("id", false);
		if (trainingResultsDTO.getState() != null){
			wrapper.eq("state",trainingResultsDTO.getState());
		}
		if (StringUtils.isNotBlank(trainingResultsDTO.getName())){
			wrapper.like("name",trainingResultsDTO.getName());
		}
		if (StringUtils.isNotBlank(trainingResultsDTO.getTpapersName())){
			Wrapper<TrainingPapersDO> Wrapper = new EntityWrapper<TrainingPapersDO>().like("name",trainingResultsDTO.getTpapersName());
			List<TrainingPapersDO>  trainingPapersDOS = trainingPapersService.selectList(Wrapper);
			String str = "";
			for (TrainingPapersDO trainingPapersDO : trainingPapersDOS){
				str = str + trainingPapersDO.getId() + ",";
			}
			wrapper.and();
			if(StringUtils.isNotBlank(str)){
				wrapper.in("tpapers_id",str.substring(0,str.length()-1));
			}else{
				wrapper.eq("tpapers_id",null);
			}
		}

		String strr = roleCourse(req,trainingResultsDTO.getCoursesName());
		if (org.apache.commons.lang.StringUtils.isBlank(strr)) {
			wrapper.and();
			wrapper.eq("courses_id", null);
		} else if(!strr.equals("@#$%^&*!~")){
			wrapper.and();
			wrapper.in("courses_id", strr.substring(0, strr.length() - 1));
		}

        Page<TrainingResultsDO> page = trainingResultsService.selectPage(getPage(TrainingResultsDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			TrainingResultsDO obj = (TrainingResultsDO) page.getRecords().get(i);
			if (obj.getTpapersId() != null){
				TrainingPapersDO trainingPapersDO = trainingPapersService.selectById(obj.getTpapersId());
				if (trainingPapersDO != null) {
					obj.setTpapersName(trainingPapersDO.getName());
				}
			}
			if (obj.getCoursesId() != null){
				CoursesDO coursesDO = coursesService.selectById(obj.getCoursesId());
				if (coursesDO != null) {
					obj.setCoursesName(coursesDO.getName());
				}
			}
			if (obj.getUserId() != null){
				UserDO userDO = userService.selectById(obj.getUserId());
				if (userDO != null) {
					obj.setUserName(userDO.getUsername());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:trainingResults:add")
	String add(){
	    return "jzweb/trainingResults/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:trainingResults:edit")
	String edit(@PathVariable("id") Long id,Model model){
		TrainingResultsDO trainingResults = trainingResultsService.selectById(id);
		model.addAttribute("trainingResults", trainingResults);
	    return "jzweb/trainingResults/edit";
	}
	
	@Log("添加培训成绩表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:trainingResults:add")
	public Result<String> save( TrainingResultsDO trainingResults){
		trainingResultsService.insert(trainingResults);
        return Result.ok();
	}
	
	@Log("修改培训成绩表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:trainingResults:edit")
	public Result<String>  update( TrainingResultsDO trainingResults){
		boolean update = trainingResultsService.updateById(trainingResults);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除培训成绩表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:trainingResults:remove")
	public Result<String>  remove( Long id){
		trainingResultsService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除培训成绩表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:trainingResults:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		trainingResultsService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
